﻿namespace Okazuki.MVVM.PrismSupport.Test.ViewModels
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using Microsoft.Practices.Prism.Commands;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Okazuki.MVVM.PrismSupport.Utils;
    using Okazuki.MVVM.PrismSupport.ViewModels;

    /// <summary>
    /// ViewModelBaseTest の概要の説明
    /// </summary>
    [TestClass]
    public class ViewModelBaseTest
    {
        // テスト用具象クラス
        class TestTargetViewModel : ViewModelBase
        {
            [AutoInit]
            public object AutoInitProperty { get; set; }

            private DelegateCommand sampleCommand;
            public DelegateCommand SampleCommand
            {
                get
                {
                    return this.sampleCommand = this.sampleCommand ??
                        new DelegateCommand(
                            () => { });
                }
            }

            private DelegateCommand sampleCommand2;
            public DelegateCommand SampleCommand2
            {
                get
                {
                    return this.sampleCommand2 = this.sampleCommand2 ??
                        new DelegateCommand(
                            () => { });
                }
            }
        }

        private TestTargetViewModel target;

        [TestInitialize]
        public void SetUp()
        {
            this.target = new TestTargetViewModel();
        }

        [TestCleanup]
        public void TearDown()
        {
            this.target = null;
        }

        [TestMethod]
        public void TestAutoInitProperty()
        {
            Assert.IsNotNull(
                this.target.AutoInitProperty,
                "AutoInit属性がついてるプロパティは初期化されている");
        }

        [TestMethod]
        public void TestCommandCanExecute()
        {
            var eventHandlers = this.target.RequerySuggestedEventHandlers;
            Assert.IsNotNull(eventHandlers, "値は取れているはず");
            Assert.AreEqual(2, eventHandlers.Count(), "イベントの数だけ要素数がある");
            Assert.AreNotEqual(
                eventHandlers.ElementAt(0),
                eventHandlers.ElementAt(1),
                "違う種類のイベントハンドラが登録されていること");
        }
    }
}
